<template>
  <div>
    <el-tree
      :data="resource"
      show-checkbox default-expand-all 
      node-key="id" 
      ref="tree"  
      highlight-current :props="defaultProps"
       >
    </el-tree>
    <div class="buttons">
  <el-button @click="getCheckedKeys">提交</el-button>
  <el-button @click="resetChecked">清空</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "Auth",
  data() {
    return {
      rid: 0,
      resource: [],
      defaultProps:{
        children:"children",
        label: "label"
     
      }
    };
  },
  props:['dialogAuth','pp']
  ,
  methods: {
    getCheckedKeys() {
      var wet=this.$refs.tree.getHalfCheckedKeys()
      var tyu= this.$refs.tree.getCheckedKeys()
      wet.forEach(res=>{
       tyu.push(res)
      })
      var cc={ids:tyu,rid:this.rid}
      this.axios({
        method:'post',
        url:'Auth',
        data:cc
      }).then(res=>{
        if(res.code>0){
          this.$message({ type:'success', message:res.msg})
          
        }else{
          this.$message({ type:'danger', message:res.msg})
        }
      })
   
      this.$emit("update:dialogAuth",false)
    }
      ,
      setCheckedKeys() {
        var uu =[]
        var opk=[1,2,3,4]
        var jk=[]
        this.axios({
          method:'get',
          url:'Auth',
          params:{rid:this.rid}
        }).then(res=>{
          res.data.forEach(d=>{
                uu.push(opk.includes(d.MenuID)?'':d.MenuID)
          })
      
          
           uu.forEach(data=>{
           if(data!=""){
          jk.push(data)
           }
           })
            console.log('jk',jk)
          this.$refs.tree.setCheckedKeys(jk);
        })
   
      },
    resetChecked() {
      this.$refs.tree.setCheckedKeys([]);
    },
    search(){
      this.rid=this.pp
    this.axios({
      method: "get",
      url: "Menus/Message",
    }).then(res=> {
      res.data.forEach(v=> {
        if (v.ParentID == 0) {
          var node = {
            id: v.MenuID,
            label: v.MenuName,
            children: []
          };
          this.resource.push(node);
          res.data.forEach(vv => {
            if (vv.ParentID == v.MenuID) {
              node.children.push({
                id: vv.MenuID,
                label: vv.MenuName
              });
            }
          });

          
        }
       
      });
     setTimeout(() => {
        this.setCheckedKeys();
      },1);

    })
  
    }
  },
  created() {
       this.search();
   
  },
  watch:{
  dialogAuth(newvalue,oldvalue){
    this.rid=this.pp
     this.setCheckedKeys();
    console.log('pp',this.pp)
   
  }
  }
 
};
</script>

<style scoped>
</style>


